package com.kfh.ybracelet.location;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CircleOptions;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.kfh.ybracelet.R;
import com.kfh.ybracelet.base.BaseActivity;
import com.kfh.ybracelet.bean.SafeAreaResult;

/**
 * 安全区域详情
 *
 * @author Aaron
 */
public class SafeAreaDetailActivity extends BaseActivity {

    public final String TAG = getClass().getSimpleName();
    private TextView tv_title;

    private AMap aMap;
    private MapView mapView;
    //    private AMapLocationClient mLocationClient;
    private LatLng mLatLng = null;
    private int mRadius;
    //    private String mSafeAreaName;
    private SafeAreaResult.AreaList mSafeAreaList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.safe_area_detail);
//        mApplication.addActivity(this);
        mSafeAreaList = (SafeAreaResult.AreaList) getIntent().getSerializableExtra("safeArea");

        // 加载控件
        initView();

        mapView = (MapView) findViewById(R.id.gdmap);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initMap();
        initValue();
        addLocationMarker();
    }


    private void addLocationMarker() {

        aMap.clear();
//        float scale = aMap.getScalePerPixel();//一像素代表多少米
//        float v = mRadius / scale;
        Log.e(TAG, "定位圈半径==mRadius==:" + mRadius);
        addCircle(mLatLng, mRadius);//添加定位圈
        addMarker(mLatLng);
//                mLocMarker.setPosition(location);

        int scal = 18; //计算地图的缩放比例
        if (mRadius <= 300) {
            scal = 17;
        } else if (mRadius > 300 && mRadius <= 500) {
            scal = 16;
        } else if (mRadius > 500 && mRadius <= 800) {
            scal = 15;
        } else if (mRadius > 800 && mRadius <= 1000) {
            scal = 14;
        }

        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, scal));//移动到location位置,并设置缩放比例

    }

    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(20, 0, 0, 180);

    private void addCircle(LatLng latlng, double radius) {

        Log.e(TAG, "精度圈的大小:" + radius);

//        CircleOptions options = new CircleOptions();
//        options.strokeWidth(1f);
////        options.fillColor(getResources().getColor(R.color.color_location));
//        options.fillColor(FILL_COLOR);
//        options.strokeColor(STROKE_COLOR);
//        options.center(latlng);
//        options.radius(radius);
//        Circle circle = aMap.addCircle(options);
//        circle.setVisible(true);

        aMap.addCircle(new CircleOptions().center(latlng).radius(radius).strokeColor(STROKE_COLOR)
                .fillColor(FILL_COLOR).strokeWidth(1f));

    }

    //    private Marker mLocMarker;
    BitmapDescriptor des = null;

    private void addMarker(LatLng latlng) {
//        if (mLocMarker != null) {
//            return;
//        }

        if (des == null) {
            Bitmap bMap = BitmapFactory.decodeResource(this.getResources(),
                    R.mipmap.thumb);
            des = BitmapDescriptorFactory.fromBitmap(bMap);
        }

//		BitmapDescriptor des = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked);
        MarkerOptions options = new MarkerOptions();
        options.icon(des);
        options.anchor(0.5f, 0.5f);
        options.position(latlng);
        aMap.addMarker(options);
//        mLocMarker.setTitle("title");
    }

    private void initMap() {
        if (aMap == null) {
            aMap = mapView.getMap();
//            setUpMap();
            //显示比例尺
            aMap.getUiSettings().setScaleControlsEnabled(true);
        }
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {
            case R.id.iv_left:

                finish();

                break;

            default:
                break;
        }

    }

    /**
     * 初始化变量
     */
    private void initValue() {
        if (mSafeAreaList != null) {
            mLatLng = new LatLng(mSafeAreaList.latitude, mSafeAreaList.longitude);

            mRadius = mSafeAreaList.radius;
        }
    }


    private void initView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText(getResources().getString(R.string.safe_area_detail));
        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        iv_left.setOnClickListener(this);
    }

//    /**
//     * 创建覆盖物
//     */
//    private void createPoint() {
//
//        aMap.clear();
////        // 添加圆
////        if (mLatLng != null) {
////            OverlayOptions ooCircle = new CircleOptions().fillColor(0x4d35a9e2)
////                    .center(mLatLng).stroke(new Stroke(5, 0x8035a9e2))
////                    .radius(mRadius);
////
////            mBaiduMap.addOverlay(ooCircle);
////
////            // 添加点
////            OverlayOptions ooDot = new DotOptions().center(mLatLng).radius(6)
////                    .color(0xff35a9e2);
////            mBaiduMap.addOverlay(ooDot);
////            // /**
////            // * 各级比例尺分母值数组 地图缩放级别 3~19
////            // */
////            // private static final int[] SCALES = { 20, 50, 100, 200, 500,
////            // 1000,
////            // 2000, 5000, 10000, 20000, 25000, 50000, 100000, 200000,
////            // 500000, 1000000, 2000000 };
////            float zoom = 19;
////            // 定位到该点
////            if (mRadius > 20 && mRadius <= 50) {
////                zoom = 18;
////            } else if (mRadius > 50 && mRadius <= 100) {
////                zoom = 17;
////            } else if (mRadius > 100 && mRadius <= 200) {
////                zoom = 16;
////            } else if (mRadius > 200 && mRadius <= 500) {
////                zoom = 15;
////            } else if (mRadius > 500 && mRadius <= 1000) {
////                zoom = 14;
////            }
////            MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(mLatLng,
////                    zoom);
////            mBaiduMap.setMapStatus(u);
////        }
//
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mapView != null) {
            mapView.onDestroy();
            mapView = null;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        if (mapView != null)
            mapView.onPause();

    }

}
